package com.bw.mallshop.mvvm.view.frament;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bw.common.mvvm.view.BaseFragment;
import com.bw.mallshop.BR;
import com.bw.mallshop.R;
import com.bw.mallshop.databinding.FragmentUserBinding;
import com.bw.mallshop.field.EventAction;
import com.bw.mallshop.mvvm.viewmodel.UserViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class UserFragment extends BaseFragment<FragmentUserBinding, UserViewModel> {

    @Override
    protected int createvariableId() {
        return BR.vm;
    }

    @Override
    public int bindlayout() {
        return R.layout.fragment_user;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        vm.refresh();
        return view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshEvent(EventAction action){
        if (action.action.equals("refresh_user")){
            vm.refresh();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
